package com.gzx.plugin.tjzy.modular.sms.batch.mapper;

import com.gzx.plugin.tjzy.modular.sms.batch.entity.GzxSmsBatch;
import com.gzx.plugin.tjzy.modular.sms.batch.vo.GzxSmsBatchVo;
import com.gzx.plugin.tjzy.modular.sms.batch.vo.GzxSmsStatisticVo;
import com.gzx.plugin.tjzy.modular.sms.batch.vo.GzxStatisticNumVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import vip.xiaonuo.common.mapper.BaseMapperPlus;

import java.util.List;

/**
 * 短信批次 gzx_sms_batch
 *
 * @author xbl
 * @date 2025-03-19
 */
public interface GzxSmsBatchMapper extends BaseMapperPlus<GzxSmsBatch, GzxSmsBatchVo> {
    /**
     * 获取供应商顺序，这批发送了多少条
     */
    @Select("<script>" +
            "select total_quantity totalQuantity,supplier_order supplierOrder from gzx_sms_batch where batch_number = #{batchNumber} limit 1" +
            "</script>")
    GzxSmsStatisticVo getQuantityOrder(@Param("batchNumber") String batchNumber);

    /**
     * 飞鸽短信发送计费，失败的数量，成功数量，未知数量
     */
    @Select("<script>" +
            " select " +
            " count(*) cnt,supplier_code as supplierCode ,'SUCCESS' as flag " +
            " FROM " +
            " gzx_sms_detail " +
            " WHERE batch_number =#{batchNumber} " +
            " and send_status = 2 " +
            " group by supplier_code " +
            " union all " +
            " select " +
            " count(DISTINCT msg_id,phone) cnt,supplier_code as supplierCode,'FAILED' as flag " +
            " FROM " +
            " gzx_sms_receipt " +
            " WHERE batch_number =#{batchNumber}  " +
            " and mapping_status = '0'" +
            " group by supplier_code " +
            " union all " +
            " SELECT " +
            " count(*) cnt,supplier_code as supplierCode,'COST' as flag " +
            " FROM " +
            " gzx_sms_receipt " +
            " WHERE batch_number = #{batchNumber}  " +
            " AND mapping_status = '1' " +
            " group by supplier_code " +
            " union all " +
            " SELECT " +
            " count(*) cnt, " +
            " supplier_code as supplierCode , 'UNKNOW' as flag " +
            " FROM " +
            " gzx_sms_detail " +
            " WHERE batch_number = #{batchNumber} " +
            " AND send_status = '1' " +
            " group by supplier_code  " +
            "</script>")
    List<GzxStatisticNumVo> getStatistic(@Param("batchNumber") String batchNumber);

    /**
     * 当前批次号发送计费，失败的数量
     */
    @Select("<script>" +
            "select" +
            "   count(if(result_code='DELIVRD',1,null)) billableNum," +
            "   count(if(result_code!='DELIVRD',1,null)) failedNum," +
            "   supplier_code supplierCode " +
            "from gzx_sms_receipt " +
            "where batch_number = #{batchNumber} " +
            "group by supplier_code " +
            "</script>")
    List<GzxSmsStatisticVo> getSuccessFailedNum(@Param("batchNumber") String batchNumber);

    /**
     * 当前批次号发送状态成功，未知数量
     */
    @Select("<script>" +
            "select " +
            " count(if(send_status = 2,1,null)) successNum ," +
            " count(if(send_status != 2,1,null)) unknowNum ," +
            " supplier_code supplierCode " +
            "from gzx_sms_detail " +
            "where batch_number = #{batchNumber} " +
            "and send_status = 1 " +
            "group by supplier_code " +
            "</script>")
    List<GzxSmsStatisticVo> getUnknowNum(@Param("batchNumber") String batchNumber);
}
